package com.bwie.bwedu.utils;

import android.annotation.SuppressLint;
import android.util.Log;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;

/**
 * 日志工具类
 * 
 * @author xiangming
 * 
 */
@SuppressLint("SimpleDateFormat")
public class LogUtils {

	public static boolean IsOpen = true;

	public static boolean IsOpenLogFile = false;

	private static final String tag_debug = "zhima";

	private static final String tag_error = "zhima";

	private static final String ErrorLogPath = "zhima/log";
	
	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");

	public static void d(String s) {
		if (IsOpen) {
			Log.d(tag_debug, s);
		}
	}

	public static void e(String e) {
		if (IsOpen) {
			Log.e(tag_error, e);
		}
	}

	public static void e(Exception ex) {
		if (IsOpen) {
			Log.e(tag_error, "exception name-------------->" + ex.getMessage());
			Writer writer = new StringWriter();
			PrintWriter printWriter = new PrintWriter(writer);
			ex.printStackTrace(printWriter);
			Throwable cause = ex.getCause();
			while (cause != null) {
				cause.printStackTrace(printWriter);
				cause = cause.getCause();
			}
			printWriter.close();
			String c = writer.toString();
			Log.e(tag_error, c);
		}
	}

//	private static void writeErrorLogFile(String e) {
//		if (IsOpenLogFile) {
//			String time = sdf.format(new Date(System.currentTimeMillis()));
//			StringBuffer buf = new StringBuffer();
//			buf.append(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>").append("\n");
//			buf.append(time).append("\n");
//			buf.append(e).append("\n");
//			buf.append("\n");
//			String filename = new StringBuffer(ErrorLogPath).append("/").append(time.substring(0, 13)).append(".txt").toString();
//			FileUtils.write(filename, buf.toString(), true);
//		}
//	}
	
}
